package com.waa.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import com.waa.gulimall.product.entity.BrandEntity;
import com.waa.gulimall.product.vo.BrandVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.waa.gulimall.product.entity.CategoryBrandRelationEntity;
import com.waa.gulimall.product.service.CategoryBrandRelationService;
import com.waa.gulimall.common.utils.R;



/**
 * 品牌分类关联
 *
 * @author waa
 * @email 2460335742@qq.com
 * @date 2022-02-22 09:57:25
 */
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    /**
     * 列表
     */
    /*
    * 获取当前品牌关联的所有分类列表
    * */
    @GetMapping("/catelog/list")
    public R cateloglist(@RequestParam("brandId")Long brandId){
        final List<CategoryBrandRelationEntity> data = categoryBrandRelationService.queryPage(brandId);
        return R.ok().put("data", data);
    }
    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);

        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
        categoryBrandRelationService.saveDetail(categoryBrandRelation);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.updateById(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		categoryBrandRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }
  /*
  * 获取分类关联的品牌
/product/categorybrandrelation/brands/list
   * */
    @GetMapping("/brands/list")
    public  R relationBrandsList(@RequestParam(value = "catId",required = true) Long catId)
    {
       List<BrandEntity> list= categoryBrandRelationService.getBrandsByCatId(catId);
        final List<BrandVo> brandVoList = list.stream().map(item -> {
            final BrandVo brandVo = new BrandVo();
            brandVo.setBrandId(item.getBrandId());
            brandVo.setBrandName(item.getName());
            return brandVo;
        }).collect(Collectors.toList());
        return R.ok().put("data",brandVoList);
    }

}
